package com.my.springboot.tuling.concurrent.cas;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 *ABA 问题解决方案：
 *数据库有个锁称为乐观锁，是一种基于数据版本实现数据同步的机制，每次修改一次数据，版本
 * 就会进行累加。
 * 同样，Java也提供了相应的原子引用类AtomicStampedReference<V>
 */
@Slf4j
public class ABATest {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(1);
        new Thread(() -> {
            int value = atomicInteger.get();
            log.debug("Thread1 read value: " + value);
            // 阻塞1s
            LockSupport.parkNanos(1000000000L);
            // Thread1通过CAS修改value值为3
            if(atomicInteger.compareAndSet(value,3)){
                log.debug("Thread1 update from " + value + " to 3");
            }else {
                log.debug("Thread1 update fail!");
            }
        },"Thread1").start();

        new Thread(() -> {
            int value = atomicInteger.get();
            log.debug("Thread2 read value: " + value);
            // Thread2通过CAS修改value值为2
            if(atomicInteger.compareAndSet(value,2)){
                log.debug("Thread2 update from " + value + " to 2");
                // do something
                value = atomicInteger.get();
                log.debug("Thread2 read value " + value );
                if(atomicInteger.compareAndSet(value,1)){
                    log.debug("Thread2 update from " + value + " to 1");
                }
            }
        },"Thread2").start();
    }
}
